<template>
	<view>
		<uni-nav-bar left-icon="back" @click-left="back" @click-right="save()" right-text="保存" title="地址修改"></uni-nav-bar>
		<view class="list">
			<view class="list-item">
				<text>收货人</text>
				<view class="input">
					<input class="inputText" type="text" data-name="name" @input="input" :value="addressData.name" placeholder="请填写收货人姓名" placeholder-class="placeholder" />
				</view>
			</view>
			<view class="list-item">
				<text>手机号</text>
				<view class="input">
					<input
						class="inputText"
						type="text"
						data-name="mobile"
						@input="input"
						:value="addressData.mobile"
						placeholder="请填写收货手机号"
						placeholder-class="placeholder"
					/>
				</view>
			</view>
			<view class="list-item" @click="showMulLinkageThreePicker">
				<text>地址</text>
				<view class="input">
					<text>{{addressData.province+addressData.city+addressData.area}}</text>
					<uni-icons type="forward" size="30" color="#999999"></uni-icons>
				</view>
			</view>
			<view class="list-item">
				<text>详细地址</text>
				<view class="input">
					<input
						class="inputText"
						type="text"
						data-name="address"
						@input="input"
						:value="addressData.address"
						placeholder="请填写详细地址"
						placeholder-class="placeholder"
					/>
				</view>
			</view>
			<view class="list-item">
				<text>设为默认地址</text>
				<switch :checked="addressData.is_default == 1" @change="changeDefault" style="transform:scale(.7);" />
			</view>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault" @="" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue';
import uniIcons from '@/components/uni-icons/uni-icons.vue';
import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
export default {
	components: { uniNavBar, uniIcons, mpvueCityPicker },
	data() {
		return {
			addressData: {
				name: '',
				mobile: '',
				province: '',
				city: '',
				area: '',
				address: '',
				is_default: 0
			},
			cityPickerValueDefault: [0, 0, 1],
			themeColor: '#007AFF'
		};
	},
	onLoad() {
		this.getData();
	},
	methods: {
		async getData() {
			let res = await this.$request.get('/setting/setMemberAddress', {});
			let data = res.data.data.list;
			if (data) {
				let { name, mobile, province, city, area, address, is_default } = data;
				this.addressData = {
					name,
					mobile,
					province,
					city,
					area,
					address,
					is_default
				};
			}
		},
		input(e) {
			let name = e.target.dataset.name;
			let value = e.detail.value;
			this.addressData[name] = value;
		},
		back() {
			uni.navigateBack({
				delta: 1
			});
		},
		async save() {
			let addressData = this.addressData;
			for (let key in addressData) {
				if (!addressData[key] && key!='is_default') {
					uni.showToast({
						title: '请将表单填写完整',
						duration: 2000
					});
					console.log(key);
					return false;
				}
			}
			let res = await this.$request.post('/setting/setMemberAddress', {
				data: {
					...addressData
				}
			});
			if (res.data.code == 10000) {
				uni.showToast({
					title: '保存成功',
					duration: 2000
				});
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					});
				}, 2000);
			} else {
				uni.showToast({
					title: '保存失败',
					duration: 2000
				});
			}
		},
		showMulLinkageThreePicker() {
			this.$refs.mpvueCityPicker.show();
		},
		onConfirm(e) {
			let label = e.label.split('-');
			let addressData = this.addressData;
			addressData.province = label[0];
			addressData.city = label[1];
			addressData.area = label[2];
			this.addressData = addressData;
		},
		changeDefault(e) {
			this.addressData.is_default = e.detail.value ? 1 : 0;
		}
	}
};
</script>

<style lang="scss">
.list {
	width: 100%;
	box-sizing: border-box;
	padding-left: 30upx;
	.list-item {
		width: 100%;
		height: 110upx;
		border-bottom: 2upx solid #e7e7e7;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding-right: 20upx;
		font-size: $uni-font-size-base;
		color: #333333;
		.input {
			width: 500upx;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			
			.inputText {
				color: #333333;
				font-size: $uni-font-size-base;
				width: 100%;
				text-align: right;
				box-sizing: border-box;
			}
		}
	}
}
.placeholder {
	color: #d7d7d7;
}
</style>
